package com.sxkf.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.template.constants.OrderConstant;
import com.sxkf.template.dto.EvaluationDto;
import com.sxkf.template.entity.*;
import com.sxkf.template.service.CommentService;
import com.sxkf.template.service.OrderService;
import com.sxkf.template.service.StoreEvaluationService;
import com.sxkf.template.service.UserService;
import com.sxkf.template.vo.request.CommentVo;
import com.sxkf.template.vo.response.PostCommentVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* (t_comment)表控制层
*
* @author zgc
*/
@Api(tags="商品评价模块")
@RestController
@RequestMapping("/comment")
public class CommentController {
    @Resource
    private CommentService commentService;

    @Resource
    private UserService userService;

    @ApiOperation(value = "查询商品评论")
    @GetMapping(value = "/page")
    public R<IPage<Comment>> page(@RequestParam Integer pageNo,
                                  @RequestParam Integer pageSize,
                                  @RequestParam String productId,
                                  @RequestParam(required = false,defaultValue = "false") Boolean hasImg,
                                  @RequestParam(required = false,defaultValue = "false") Boolean hasVideo){
        Page<Comment> page = commentService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<Comment>()
                .eq(Comment::getProductId, productId)
                .eq(Comment::getPid, "0")
                .isNotNull(hasImg,Comment::getImageUrl)
                .isNotNull(hasVideo,Comment::getVideoUrl));

        List<String> userIdList = page.getRecords().stream().map(Comment::getUserId).collect(Collectors.toList());
        if (userIdList.isEmpty()){
            return R.success(page);
        }
        List<User> userList = userService.list(new LambdaQueryWrapper<User>().in(User::getId, userIdList));
        Map<String, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, user -> user));
        page.getRecords().forEach(item-> {
            item.setChildCount(commentService.count(new LambdaQueryWrapper<Comment>()
                .eq(Comment::getTopId, item.getId())));
            User user = userMap.get(item.getUserId());
            item.setUserName(user.getNickName());
            item.setUserAvatar(user.getAvatar());
        });
        return R.success(page);
    }


    @ApiOperation(value = "查询某条评论的子评论")
    @GetMapping(value = "/listChild")
    public R<IPage<Comment>> listChildComments(@RequestParam Integer pageNo,
                                                   @RequestParam(defaultValue = "10") Integer pageSize,
                                                   @RequestParam String commentPid) {
        Page<Comment> result = commentService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<Comment>()
                .eq(Comment::getTopId, commentPid)
                .orderByDesc(Comment::getCreateTime));

        List<String> userIdList = result.getRecords().stream().map(Comment::getUserId).collect(Collectors.toList());
        if (userIdList.isEmpty()){
            return R.success(result);
        }
        List<User> userList = userService.list(new LambdaQueryWrapper<User>().in(User::getId, userIdList));
        Map<String, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, user -> user));
        result.getRecords().forEach(item-> {
            item.setChildCount(commentService.count(new LambdaQueryWrapper<Comment>()
                    .eq(Comment::getTopId, item.getId())));
            User user = userMap.get(item.getUserId());
            item.setUserName(user.getNickName());
            item.setUserAvatar(user.getAvatar());
        });

        return R.success(result);
    }

    @ApiOperation(value = "查询某条评论")
    @GetMapping(value = "/oneComment")
    public R<Comment> oneComment(@RequestParam String commentId) {
        Comment comment = commentService.getById(commentId);
        return R.success(comment);
    }

    @ApiOperation(value = "某条评论点赞")
    @PostMapping(value = "/like")
    public R<String> likeById(@RequestBody CommentLike commentLike) {
        String res = commentService.likeById(commentLike);
        return R.success(res);
    }

    @ApiOperation(value = "某条评论是否点赞")
    @PostMapping(value = "/isLike")
    public R<Boolean> isLike(@RequestParam String userId,
                             @ApiParam("类型（1=帖子评论，2=商品评论）")
                             @RequestParam String type,
                             @RequestParam String commentId) {
        Boolean res = commentService.isLike(userId,type,commentId);
        return R.success(res);
    }

    @ApiOperation(value = "查详情")
    @GetMapping(value = "/one")
    public R<Comment> one(@RequestParam String id){
        Comment comment = commentService.getById(id);
        return R.success(comment);
    }

    @ApiOperation(value = "添加评论")
    @PostMapping(value = "/add")
    public R<?> add(@RequestBody Comment comment){
        //todo 添加评分
        commentService.save(comment);
        return R.success();
    }

    @ApiOperation(value = "去评论")
    @PostMapping(value = "/addComment")
    public R<?> addComment(@RequestBody CommentVo comment){

        Boolean res = commentService.addComment(comment);

        return R.success();
    }


    @ApiOperation("我的商品评价")
    @GetMapping("/myComment")
    public R<IPage<Comment>> myComment(@RequestParam Integer pageNo,
                                       @RequestParam Integer pageSize,
                                       @RequestParam Long userId){
        Page<Comment> result = commentService.myComment(pageNo, pageSize, userId);
        return R.success(result);
    }

    @ApiOperation("谁的订单的商品的评价")
    @GetMapping("/productComment")
    public R<IPage<Comment>> productComment(@RequestParam Integer pageNo,
                                       @RequestParam Integer pageSize,
                                       @RequestParam String orderId,
                                       @RequestParam Long userId){

        Page<Comment> result = commentService.productComment(pageNo, pageSize, orderId, userId);

        return R.success(result);
    }




}
